package com.example.springtest.grade.service.impl;



import com.example.springtest.common.ServiceCode;
import com.example.springtest.common.ServiceException;
import com.example.springtest.course.mapper.CourseMapper;
import com.example.springtest.course.pojo.entity.Course;
import com.example.springtest.exam.mapper.ExamMapper;
import com.example.springtest.exam.pojo.Exam;
import com.example.springtest.grade.mapper.GradeMapper;
import com.example.springtest.grade.pojo.Grade;
import com.example.springtest.grade.service.GradeService;
import com.example.springtest.people.mapper.PeopleMapper;
import com.example.springtest.people.pojo.entity.People;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class GradeServiceImpl implements GradeService {

    @Autowired
    private GradeMapper gradeMapper;
    @Autowired
    private PeopleMapper peopleMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private ExamMapper examMapper;


    /**
     * 显示所有成绩
     * @return
     */
    @Override
    public List<Grade> getGradeList(){
        List<Grade> gradeList=gradeMapper.getList();
        return gradeList;
    }

    /**
     * 增加成绩
     * @param grade
     */
    @Override
    public void addGrade(Grade grade){

        People people= peopleMapper.getByName(grade.getStu_name());
        if(people==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"不存在该学生！");
        }
        Course course =courseMapper.getByName(grade.getCo_name());
        if(course==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"不存在该课程!");
        }
        Exam exam=examMapper.getByName(grade.getCo_name());
            if(exam==null){
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"不存在该考试!");
            }
            if(exam.getStatus().equals("未完成")){
                throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"考试未结束!");
            }
     gradeMapper.save(grade);
    }

    /**
     * 删除成绩
     * @param id
     */
    @Override
    public void deleteGrade(Integer id){
        Grade grade = gradeMapper.getById(id);
        if(grade==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"不存在该成绩！");
        }
        int result=gradeMapper.delete(id);
        if(result!=1){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"删除失败！");
        }
    }

    /**
     * 编辑成绩
     * @param id
     * @param grade
     */
    @Override
    public void editGarde(Integer id,Grade grade){
        Grade grade1 = gradeMapper.getById(id);
        if(grade1==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"不存在该成绩！");
        }
        People people= peopleMapper.getByName(grade.getStu_name());
        if(people==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"不存在该学生！");
        }
        Course course =courseMapper.getByName(grade.getCo_name());
        if(course==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"不存在该课程!");
        }
        grade1.setCo_name(grade.getCo_name());
        grade1.setStu_name(grade.getStu_name());
        grade1.setGrade(grade.getGrade());
        int result=gradeMapper.edit(grade1);
        if(result!=1){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"编辑失败！");
        }
    }
}
